Welcome to the solution for coding exercise 15 and we import pen as an umpire.
Matt plop plop.
And then we import our six stocks and we should select to the appropriate price data to calculate total
returns.
And therefore we selected the adjusted close column because uh the adjusted clause includes dividends.
And then we select the five year period from two 14 to 18 and be over right stocks
so we we have a five year period for our six stocks and later we will need to calculate annualized risk
and return and to make life easier.
We can define you a user defined function annualized the risk and return.
And we actually pass a data frame with daily returns uh to the user defined function and the function
actually calculates uh the mean return and the standard deviation of returns and we can do this here
with the ACT method and uh then the risk and return are getting annualized.
So that's the user defined function.
And first of all we have to create the returns data frame and we can simply do the service of the percentage
change method on the prices on the stock prices.
So that's the return data frame with daily returns.
And then we can pass the return data frame to our user defined function and we create the summary data
frame with annualized the risk and return for our six stocks.
And then we can also visualize the risk and return in a total risk return framework.
So we simply running the cell
and we can see that the maximally the impact on McDonald's stock showed a quite good performance here
on the upper left corner.
And in contrast American Express is pretty weak.
And actually Facebook showed high risk but also high return.
Now the next step we want to create 10000 random portfolios with random weights of the constituents
and uh therefore we define that the number of assets as six and the number of random portfolios 10000
and then we create a number higher rate with 10000 roles and six columns and each element is a random
float between 0 and 1 and we save the matrix and the variable matrix and actually we use the other random
seed.
1 2 3.
And by doing so you should get the very same results as I do here in the solution.
So we you see an umpire dot random dot random and create the 60000 the random floats between 0 and 1
and we reshape those and 2 10000 rows and 6 columns
and then for each row for each random portfolio we can create the weights that sum up to 1 and we can
simply do this by dividing each and every element in a row by the sum of the row and here we have the
weights array and we can also double check if the weights in each row sum up to 1 and this is obviously
the case here so now we have far 10000 random portfolios.
Ten thousand sets of random weights.
And uh we can calculate actually the daily returns of uh these random portfolios by using here the top
method on the returns data frame and pass the transpose the weights matrix.
So this is a matrix multiplication and.
We save the resulting data frame and the rabbit portfolio returns.
So here we have for 10000 random portfolios of the daily returns.
And then we can also annualize the risk and return for the 10000 portfolios.
And we actually save for the resulting data frame and the value portfolio summary.
So we simply pass here the portfolio returns data frame to our user defined function.
And let's have a look here.
So this takes a while
and here we have the last five portfolios with annualized the risk and return.
And then we can also visualize the six constituents and the 10000 the random portfolios here with the
plotting the risk on the x axis and the return on the y axis.
And the same we can do for the constituents
so that's the cloud of random portfolios and to really verify the performance of the random portfolios
in terms of risk and return.
We have to calculate Sharpe ratio and uh therefore we have to define the risk free return of the risk
free asset and we use the five year US Treasury note that was issued at the end of 213 and the coupon
rate was one point seven percent.
And by definition the risk free risk of the risk free asset is a zero.
So we say if risk free return and risk free risk here in a list and we are still our summary data frame
with our constituents and then we can calculate the sharp ratio of our six constituents by adding an
additional column sharp and we can actually calculate the sharp racial bias of selecting the risk we
return from the return of the constituents divided by the risk and let's have a look here.
The question here is which stocks showed the highest chop ratio.
And obviously at this McDonald's and the same we can also do for our Random portfolios so we calculated
the SAP ratio and we add another column sharp to our portfolio summary data frame A.B. f the last five
random portfolios and the sharp ratio and the question is also what is the maximum Sharpe ratio and
we can determine this with the described method and we can see that the maximum value in the column
Sharp is the one.
So this is the highest Sharpe Ratio and again we can plot our visualize the random portfolios and the
constituents and we can actually create a third I mentioned the sharp Ray Shaw and the color will indicate
whether the portfolio has a high or low Sharpe ratio.
And it is always a bit tricky to select the appropriate values for women and we mix.
But you can see here in the shop ratio column the highest Sharpe Ratio is 1.
So we could pass the one to We Max and the minimum value here is so point 3 4.
So we could use here maybe 0 point 5 for movement but this they are actually a trial and error process
to get the most meaningful plot.
So let's try this out here.
And here we can see that here we have the portfolios with the highest chop ratios and being a dark red
actually here the next question we have to get the ROE label off the max Sharpe Ratio portfolio and
we can do this year with the idea X Max method on the shop column and by doing so we're getting the
ROE label of the max operational portfolio and this is four thousand five hundred fifty five.
And then we can also select that particular portfolio by passing five thousand five hundred fifty five
to the local operator here.
And here we have the return of 16 percent and the risk of 14 percent and the sharp ratio of 1 and then
we can also get the rates of the constituents and the max Sharpe Ratio portfolio by passing here their
own label which is also here the ROE index position.
To the weights matrix are weights array and these are actually the weights and to make this more explicit.
We can also add uh the stock takers and created a panel series.
So we are 5 percent for American Express 16 for Facebook and for example 45 for McDonald's.
And the question is Where do you think that the Sox American Express Procter and Gamble and Wal-Mart
have any weights or weights higher than zero.
And the real Max sharp Rachel portfolio and either we can you see uh optimization algorithms or we can
simply increase the number of random portfolios and uh let's do this here.
So we could increase to 50000 thousand and rerun the says here.
So here we increase the number of portfolios to 50000
so I free run our coding SATs and our max Sharpe Ratio portfolio has now a return of 18 percent and
a risk of 15 percent and a sharp ratio of 4 one point 0 4 and let's have a look at the weights.
So obviously we have zero weight for American Express for Procter and Gamble and from Walmart and we
have kind of an concentrated position in the McDonald's stock and that's also if I look at the weights
of the max Sharpe ratio that can be derived with optimization algorithms.
So here are the weights and let's create a panel series and let's compare here.
So indeed we have zero weight in those uh three stocks and we have even a higher concentration in McDonald's.
So 62 percent.
And even if this was the very best portfolio in the past it's uh not really optimal because it's pretty
much concentrated in the McDonald's stock so nobody would invest in this portfolio here in a forward
looking case and for the time being we are not finished yet with exercise 15 and I hope to see you also
in the next exercise by.